<?php
include_once('Logger.php');
class DBHelper {

	var $serverName = "localhost";
	var $userName = "root";
	var $userPassword = "kongge123";
	var $dbName = "bookstore";
	var $conn = null;

    /**
     * @throws Exception
     */
    function connectDB() {
		if ($this->conn != null) {
			throw new Exception("请勿重复初始化", 1);
		}
		$this->conn = new PDO("mysql:host=$this->serverName;dbname=$this->dbName;", $this->userName, $this->userPassword);
		$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	}

	function disconnectDB() {
		$this->conn = null;
	}

	function exeSQL($sqlStr) {
        Logger::i($sqlStr);
		$stmt = $this->conn->prepare($sqlStr); 
    	$stmt->execute();
	    $stmt->closeCursor();
	}
	
	function selectSQL($sqlStr) {
		Logger::i($sqlStr);
		$stmt = $this->conn->prepare($sqlStr); 
    	$stmt->execute();
	    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
	    $stmt->closeCursor();
	    return $data;
	}
}

?>